﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarchEstate.Manager.BusinesEntity;
using System.Data;
using Newtonsoft.Json;


public class ServiceItemController : AbstractJqueryController
{
    public void GetStatItemData()
    {
        AbstractServiceItem serviceitem = NewObject<AbstractServiceItem>();
        DataTable data = serviceitem.GetStatItemData();
        TxtJson = base.DataTableToJson(data);
    }

    public void GetServiceItemData()
    {
        AbstractServiceItem serviceitem = NewObject<AbstractServiceItem>();
        string statCode=null;
        string type=null;
        if (ParamsData.ContainsKey("_statCode") == false)
            statCode = "";
        else
        {
            string[] codes = ParamsData["_statCode"].Split(new char[] { ',' });
            for (int i = 0; i < codes.Length; i++)
            {
                if (codes[i] == "")
                    statCode = "";
                else
                {
                    if (statCode == null)
                        statCode = "'" + codes[i] + "'";
                    else
                        statCode += ",'" + codes[i] + "'";
                }
            }
        }
        if (ParamsData.ContainsKey("_type") == false)
            type = "";
        else
            type = ParamsData["_type"];
        TxtJson = base.ListToJson(serviceitem.GetServiceItemList(statCode, type));
    }

    public void SaveStatItem()
    {
        try
        {
            string code = FormData["Code"];
            string statname = FormData["StatName"];
            string type = FormData["type"];
            AbstractServiceItem serviceitem = NewObject<AbstractServiceItem>();
            serviceitem.SaveStatItem(type,code, statname);
            TxtJson = ReturnSuccess("");
        }
        catch (Exception ex)
        {
            TxtJson = ReturnError(ex.Message);
        }
    }

    public void DeleteStatItem()
    {
        try
        {
            string code = ParamsData["Code"];
            AbstractServiceItem serviceitem = NewObject<AbstractServiceItem>();
            serviceitem.DeleteStatItem(code);
            TxtJson = ReturnSuccess("删除成功！");
        }
        catch (Exception ex)
        {
            TxtJson = ReturnError(ex.Message);
        }
    }

    public void GetStatItemList()
    {
        AbstractServiceItem serviceitem = NewObject<AbstractServiceItem>();
        DataTable data = serviceitem.GetStatItemData();
        TxtJson = JavaScriptConvert.SerializeObject(data);
    }

    public void SaveServiceItem()
    {
        try
        {
            string type = FormData["type"];
            AbstractServiceItem serviceitem = GetModelToForm<AbstractServiceItem>();
            serviceitem.Save(type);
            TxtJson = ReturnSuccess("");
        }
        catch (Exception ex)
        {
            TxtJson = ReturnError(ex.Message);
        }
    }
}
 
